<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="序号" prop="sequenceNumber">
        <el-input v-model="formData.sequenceNumber" placeholder="请输入序号" />
      </el-form-item>
       <el-form-item label="出库单号码" prop="outboundNo">
        <el-input v-model="formData.outboundNo" placeholder="请输入出库单号码" />
      </el-form-item>
      <el-form-item label="标签号" prop="tagNumber">
        <el-input v-model="formData.tagNumber" placeholder="请输入标签号" />
      </el-form-item>
      <el-form-item label="入库单id" prop="inboundId">
        <el-input v-model="formData.inboundId" placeholder="请输入入库单id" />
      </el-form-item>
      <el-form-item label="入库单号码" prop="inboundNo">
        <el-input v-model="formData.inboundNo" placeholder="请输入入库单号码" />
      </el-form-item>
      <el-form-item label="入库单明细id" prop="inboundDetailId">
        <el-input v-model="formData.inboundDetailId" placeholder="请输入入库单明细id" />
      </el-form-item>
      <el-form-item label="仓库id" prop="warehouseId">
        <el-input v-model="formData.warehouseId" placeholder="请输入仓库id" />
      </el-form-item>
      <el-form-item label="仓库编号" prop="warehouseCode">
        <el-input v-model="formData.warehouseCode" placeholder="请输入仓库编号" />
      </el-form-item>
      <el-form-item label="仓库名称" prop="warehouseName">
        <el-input v-model="formData.warehouseName" placeholder="请输入仓库名称" />
      </el-form-item>
      <el-form-item label="物料id" prop="materialId">
        <el-input v-model="formData.materialId" placeholder="请输入物料id" />
      </el-form-item>
      <el-form-item label="物料名称" prop="materialName">
        <el-input v-model="formData.materialName" placeholder="请输入物料名称" />
      </el-form-item>
      <el-form-item label="规格" prop="specification">
        <el-input v-model="formData.specification" placeholder="请输入规格" />
      </el-form-item>
      <el-form-item label="型号" prop="model">
        <el-input v-model="formData.model" placeholder="请输入型号" />
      </el-form-item>
      <el-form-item label="单位" prop="unit">
        <el-input v-model="formData.unit" placeholder="请输入单位" />
      </el-form-item>
      <el-form-item label="领用数量" prop="quantityUsed">
        <el-input v-model="formData.quantityUsed" placeholder="请输入领用数量" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" placeholder="请输入备注" />
      </el-form-item>
      <el-form-item label="生产单id" prop="productionOrderId">
        <el-input v-model="formData.productionOrderId" placeholder="请输入生产单id" />
      </el-form-item>
      <el-form-item label="生产单号" prop="productionOrderNo">
        <el-input v-model="formData.productionOrderNo" placeholder="请输入生产单号" />
      </el-form-item>
      <el-form-item label="单价" prop="unitPrice">
        <el-input v-model="formData.unitPrice" placeholder="请输入单价" />
      </el-form-item>
      <el-form-item label="金额" prop="amount">
        <el-input v-model="formData.amount" placeholder="请输入金额" />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input v-model="formData.productName" placeholder="请输入产品名称" />
      </el-form-item>
      <el-form-item label="需求数" prop="requiredQuantity">
        <el-input v-model="formData.requiredQuantity" placeholder="请输入需求数" />
      </el-form-item>
      <el-form-item label="领用数" prop="receivedQuantity">
        <el-input v-model="formData.receivedQuantity" placeholder="请输入领用数" />
      </el-form-item>
      <el-form-item label="未领用数" prop="unreceivedQuantity">
        <el-input v-model="formData.unreceivedQuantity" placeholder="请输入未领用数" />
      </el-form-item>
      <el-form-item label="成品入库单id" prop="finishedGoodsInboundId">
        <el-input v-model="formData.finishedGoodsInboundId" placeholder="请输入成品入库单id" />
      </el-form-item>
      <el-form-item label="成品入库单号码" prop="finishedGoodsInboundNo">
        <el-input v-model="formData.finishedGoodsInboundNo" placeholder="请输入成品入库单号码" />
      </el-form-item>
      <el-form-item label="拼单信息" prop="combinedOrderInfo">
        <el-input v-model="formData.combinedOrderInfo" placeholder="请输入拼单信息" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { StockMaterialOutboundApi } from '@/api/erp/stockmaterialoutbound'

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  sequenceNumber: undefined,
  outboundId: undefined,
  outboundNo: undefined,
  tagNumber: undefined,
  inboundId: undefined,
  inboundNo: undefined,
  inboundDetailId: undefined,
  warehouseId: undefined,
  warehouseCode: undefined,
  warehouseName: undefined,
  materialId: undefined,
  materialName: undefined,
  specification: undefined,
  model: undefined,
  unit: undefined,
  quantityUsed: undefined,
  remark: undefined,
  productionOrderId: undefined,
  productionOrderNo: undefined,
  unitPrice: undefined,
  amount: undefined,
  productName: undefined,
  requiredQuantity: undefined,
  receivedQuantity: undefined,
  unreceivedQuantity: undefined,
  finishedGoodsInboundId: undefined,
  finishedGoodsInboundNo: undefined,
  combinedOrderInfo: undefined
})
const formRules = reactive({
  outboundId: [{ required: true, message: '出库单id不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number, outboundId: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  formData.value.outboundId = outboundId
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await StockMaterialOutboundApi.getMaterialOutboundDetail(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value
    if (formType.value === 'create') {
      await StockMaterialOutboundApi.createMaterialOutboundDetail(data)
      message.success(t('common.createSuccess'))
    } else {
      await StockMaterialOutboundApi.updateMaterialOutboundDetail(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    sequenceNumber: undefined,
    outboundId: undefined,
    outboundNo: undefined,
    tagNumber: undefined,
    inboundId: undefined,
    inboundNo: undefined,
    inboundDetailId: undefined,
    warehouseId: undefined,
    warehouseCode: undefined,
    warehouseName: undefined,
    materialId: undefined,
    materialName: undefined,
    specification: undefined,
    model: undefined,
    unit: undefined,
    quantityUsed: undefined,
    remark: undefined,
    productionOrderId: undefined,
    productionOrderNo: undefined,
    unitPrice: undefined,
    amount: undefined,
    productName: undefined,
    requiredQuantity: undefined,
    receivedQuantity: undefined,
    unreceivedQuantity: undefined,
    finishedGoodsInboundId: undefined,
    finishedGoodsInboundNo: undefined,
    combinedOrderInfo: undefined
  }
  formRef.value?.resetFields()
}
</script>